package com.wj.jiangan.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wj.jiangan.pojo.Order;
import com.wj.jiangan.pojo.Ordermx;
import com.wj.jiangan.pojo.vo.OrderVo;
import com.wj.jiangan.service.IClientService;
import com.wj.jiangan.service.IOrderService;
import com.wj.jiangan.service.IPartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wj
 * @since 2019-11-05
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IPartService partService;

    @Autowired
    private IClientService clientService;

    @PostMapping("/")
    public Map<String, Object> saveOrder(Integer eid, Integer cid) {
        Order order = new Order().setEId(eid).setCId(cid).
                setRIs(1).setRTime(LocalDateTime.now().toString());
        int i = orderService.saveOrder(order);
        Map<String, Object> result = new HashMap<>();
        result.put("code", i);
        return result;
    }

    /**
     * @param page
     * @param ris  1代表订单已下单但没有确定。2代表已确定。0代表订单已废弃
     * @param pid  部门id
     * @return
     */
    @GetMapping("/")
    public Map<String, Object> queryOrderByPage(
            Page page,
            @RequestParam(required = false) Integer ris,
            @RequestParam(required = false) Integer pid,
            @RequestParam(required = false) Integer eid,
            @RequestParam(required = false) Integer cid) {
        Map<String, Object> param = new HashMap<>();
        param.put("ris", ris);
        param.put("pid", pid);
        param.put("eid", eid);
        param.put("cid", cid);

        IPage<OrderVo> orderByPage = orderService.findOrderByPage(page, param);
        List<Map<String, Object>> list = new ArrayList<>();
        orderByPage.getRecords().forEach(o -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("rid", o.getOrder().getRId());
            map.put("ris", o.getOrder().getRIs());
            map.put("rtime", o.getOrder().getRTime());
            map.put("cname", o.getClient().getCName());
            map.put("etruename", o.getEmp().getETruename());
            list.add(map);
        });

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("count", orderByPage.getTotal());
        result.put("data", list);

        return result;
    }

    @GetMapping("/orderdetails")
    private Map<String, Object> queryOrderDetailByPage(Page page, @RequestParam Integer rid) {


        IPage<OrderVo> orderDetailPage = orderService.queryOrderDetailByPage(page, rid);

        List<Map<String, Object>> list = orderDetailPage.getRecords().stream().map(o -> {
            HashMap<String, Object> map = new HashMap<>();
            Ordermx ordermx = o.getOrdermx();
            map.put("xid", ordermx.getXId());
            map.put("count", ordermx.getXCount());
            map.put("price", ordermx.getXPrice());
            map.put("pname", o.getProduct().getPName());
            map.put("parea", o.getProduct().getPArea());
            map.put("yname", o.getProductyear().getYName());
            map.put("money", ordermx.getXCount() * ordermx.getXPrice());
            return map;
        }).collect(Collectors.toList());

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("count", orderDetailPage.getTotal());
        result.put("data", list);
        return result;
    }


}
